package
{
	import flash.utils.Dictionary;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import events.AssetsEvent;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.net.URLRequest;
	import flash.system.ApplicationDomain;

	/**
	 * @author maliu
	 */
	public class AssetsManager extends EventDispatcher
	{
		private static var m_instance:AssetsManager;
		private var m_appDomain:ApplicationDomain;
		private var m_imageCacheList:Dictionary;
		
		public function AssetsManager(_param:Param)
		{
			m_imageCacheList = new Dictionary(true);
		}
		public static function GetInstance():AssetsManager
		{
			if(!m_instance)
			{
				m_instance = new AssetsManager(new Param());
			}
			return m_instance;
		}
		//********************* API ********************************
		public function loadSwf(_url:String):void
		{
			var t_loader:Loader = new Loader();
			t_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, swfLoadedHandler);
			t_loader.load(new URLRequest(_url));
		}
		public function loadImage(_url:String):void
		{
			if(m_imageCacheList[_url] != null)
			{
				var t_event:AssetsEvent = new AssetsEvent(AssetsEvent.LOAD_COMPLETE, AssetsEvent.POLICY_CACHE_BITMAPDATA);
				t_event.loadedData = (m_imageCacheList[_url] as BitmapData).clone();
				this.dispatchEvent(t_event);
			}
			else
			{
				var t_loader:Loader = new Loader();
				t_loader.name = _url;
				t_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoadedHandler);
				t_loader.load(new URLRequest(_url));
			}
		}
		public function getAssetByClass(_className:String):Object
		{
			var t_result:Object;
			try
			{
				var t_classRef:Class = m_appDomain.getDefinition(_className) as Class;
				t_result = new t_classRef();
			}
			catch(e:Error)
			{
				trace("不存在此类定义");
			}
			return t_result;
		}
		//******************** Handler ******************************
		private function imageLoadedHandler(e:Event):void
		{
			trace("[AssetsManager] --> imageLoadedHandler");
			var t_loaderInfo:LoaderInfo = e.currentTarget as LoaderInfo;
			var t_name:String = t_loaderInfo.loader.name;
			var t_bmd:BitmapData = (t_loaderInfo.content as Bitmap).bitmapData;
			m_imageCacheList[t_name] = t_bmd;
			this.dispatchEvent(new AssetsEvent(AssetsEvent.IMAGE_LOADED_COMPLETE, t_bmd));
			t_loaderInfo.removeEventListener(Event.COMPLETE, imageLoadedHandler);
		}
		private function swfLoadedHandler(e:Event):void
		{
			trace("[AssetsManager] --> swfLoadedHandler");
			var t_loaderInfo:LoaderInfo = e.currentTarget as LoaderInfo;
			m_appDomain = t_loaderInfo.applicationDomain;
//			this.dispatchEvent(new AssetsEvent(AssetsEvent.LOAD_COMPLETE, AssetsEvent.POLICY_CACHE_APPDOMAIN));
			this.dispatchEvent(new AssetsEvent(AssetsEvent.SWF_LOADED_COMPLETE));
			t_loaderInfo.removeEventListener(Event.COMPLETE, swfLoadedHandler);
		}
	}
}
class Param{}
